<?php
/*
 * Dynmic_menu.php
 */
class Dynamic_menu {

    private $ci;            // para CodeIgniter Super Global Referencias o variables globales
    private $id_menu        = 'class="roundfg"';
    private $class_menu        = 'id="main-nav"';
    private $class_parent    = '"class="main-link';
    private $class_last        = 'class="last"';
    // --------------------------------------------------------------------
    /**
     * PHP5        Constructor
     *
     */
    function __construct()
    {
        $this->ci =& get_instance();    // get a reference to CodeIgniter.
    }
    // --------------------------------------------------------------------

    /**
     * build_menu($table, $type)
     *
     * Description:
     *
     * builds the Dynaminc dropdown menu
     * $table allows for passing in a MySQL table name for different menu tables.
     * $type is for the type of menu to display ie; topmenu, mainmenu, sidebar menu
     * or a footer menu.
     *
     * @param    string    the MySQL database table name.
     * @param    string    the type of menu to display.
     * @return    string    $html_out using CodeIgniter achor tags.
     */
   
	function build_menu($type)
    {
        $menu = array();

	 $query = $this->ci->db->query("select * from t_menu order by name ASC"); 

        // now we will build the dynamic menus.
		  $html_out  = "\t".'<div id="wrap"><div id="main-handle"><div '.$this->id_menu.'>'."\n";

        /**
         * check $type for the type of menu to display.
         *
         * ( 0 = top menu ) ( 1 = horizontal ) ( 2 = vertical ) ( 3 = footer menu ).
         */
        switch ($type)
        {
            case 0:            // 0 = top menu
                break;

            case 1:            // 1 = horizontal menu
                $html_out .= "\t\t".'<ul '.$this->class_menu.'>'."\n";
                break;

            case 2:            // 2 = sidebar menu
                break;

            case 3:            // 3 = footer menu
                break;

            default:        // default = horizontal menu
                $html_out .= "\t\t".'<ul '.$this->class_menu.'>'."\n";

                break;
        }
 
    // me despliega del query los rows de la base de datos que deseo utilizar
      foreach ($query->result() as $row)
            {
                $id = $row->id;
                $title = $row->name;
                $link_type = '';
                $module_name = '';
                $url = $row->alias;
				 $icon = $row->icon;
                $uri = '';
				$parent_id = $row->parent_id;
                $show_menu = $row->published;
				$target = '';
	
	          {
                if ($show_menu == 1 && $parent_id == 0)   // are we allowed to see this menu?

				{
				 
                  
                    // CodeIgniter's anchor(uri segments, text, attributes) tag.
         
$html_out .= "\t\t\t".'<li>'.anchor($url.' '.$this->class_parent, '<span data-icon="'.$icon.'"> '.$title.'</span>', 'name="'.$title.'" id="'.$id.'"');
                 
					
                       
			   }
          
		 	 }
		   $html_out .= $this->get_childs($id);      
          // print_r($id);		   
		}
		 // loop through and build all the child submenus.
                  
	    $html_out .= '</li><div class="close" title="Click to Collapse" >X</a></div>
'."\n";
        $html_out .= "\t\t".'</ul>' . "\n";
        $html_out .= "\t".'</div></div></div><br /><div id="sub-link-bar"> </div>' . "\n";

        return $html_out;
    }  
	 /**
     * get_childs($menu, $parent_id) - SEE Above Method.
     *
     * Description:
     *
     * Builds all child submenus using a recurse method call.
     *
     * @param    mixed    $id
     * @param    string    $id usuario
     * @return    mixed    $html_out if has subcats else FALSE
     */
    private function get_childs($id) 
    { 
		$has_subcats = FALSE;

        $html_out  = '';
        $html_out .= "\n\t\t\t\t".''."\n";
        $html_out .= "\t\t\t\t\t".'<ul class="sub-links">'."\n";
		
		// query q me ejecuta el submenu filtrando por usuario y para buscar el submenu segun el id que traigo
	     $query = $this->ci->db->query("select * from t_menu where parent_id = $id and published='1' order by ordering ASC");
		
		 foreach ($query->result() as $row)
            {
                $id = $row->id;
                $title = $row->name;
                $link_type = '';
                $module_name = '';
                $url = $row->alias;
                $uri = '';
				$parent_id = $row->parent_id;
                $show_menu = $row->published;
				$target = '';
			

		        $has_subcats = TRUE;

                if ($parent_id != 0)
                {
      $html_out .= "\t\t\t\t\t\t".'<li>'.anchor($url.'', '<span>'.$title.'</span>', 'name="'.$title.'" id="'.$id.'" target="'.$target.'"');
             
				}
                else
                {
                   $html_out .= "\t\t\t\t\t\t".'<li>'.anchor($url, '<span>'.$title.'</span>', 'name="'.$title.'" id="'.$id.'" target="'.$target.'"');
	            }

                // Recurse call to get more child submenus.
                   $html_out .= $this->get_childs($id); 
		}		
	  $html_out .= '</li>' . "\n";
	  $html_out .= "\t\t\t\t\t".'</ul>' . "\n";
      $html_out .= "\t\t\t\t".'' . "\n";

        return ($has_subcats) ? $html_out : FALSE;
	
    }
}

// ------------------------------------------------------------------------
// End of Dynamic_menu Library Class.

// ------------------------------------------------------------------------
/* End of file Dynamic_menu.php */
/* Location: ../application/libraries/Dynamic_menu.php */  